package cn.lh.pet.controller;

import cn.lh.org.exception.BusinessException;
import cn.lh.pet.dto.AcceptDto;
import cn.lh.pet.service.ISearchMasterMsgService;
import cn.lh.pet.domain.SearchMasterMsg;
import cn.lh.pet.query.SearchMasterMsgQuery;
import cn.lh.basic.util.JsonResult;
import cn.lh.basic.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     *
     * @param searchMasterMsg 传递的实体
     * @return JsonResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return JsonResult.set(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "操作失败");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return JsonResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.del(id);
            return JsonResult.set(true, "批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "删除失败");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return JsonResult 响应给前端
     */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids) {
        try {
            searchMasterMsgService.batchDelete(ids);
            return JsonResult.set(true, "批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id") Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<SearchMasterMsg> findAll() {
        return searchMasterMsgService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public Page<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest req) {
        try {
            searchMasterMsgService.publish(searchMasterMsg, req);
            return JsonResult.set(true, "发布成功");
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.set(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "系统错误稍后重试");
        }
    }

    @PostMapping("/user")
    public Page<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req) {
        return searchMasterMsgService.userSearchMasterMsg(query, req);
    }

    @PostMapping("/toHandle")
    public Page<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req) {
        return searchMasterMsgService.toHandle(query, req);
    }

    @GetMapping("/reject/{id}")
    public JsonResult reject(@PathVariable("id") long id) {
        try {
            searchMasterMsgService.reject(id);
            return JsonResult.set(true, "发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "系统错误稍后重试");
        }
    }
    @PostMapping("/accept")
    public JsonResult accept(@RequestBody AcceptDto acceptDto) {
        try {
            searchMasterMsgService.accept(acceptDto);
            return JsonResult.set(true, "发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "系统错误稍后重试");
        }
    }
}
